import grails.util.Environment

class JsTagLib {
  static namespace = "js"

  String prefix = 'mms'
  // 构建js和css代码，并根据不同的环境，生成不同引用地址。 
  private String build(type,src) {

        def appContext = request.getContextPath();  
        appContext = appContext ? appContext : System.getProperty("app.context")
        appContext = appContext ? appContext : grailsApplication.config?.grails?.app?.context
        appContext = appContext ? appContext :  grailsApplication.metadata['app.name']
        appContext = appContext == "/" ? "" : "/" + appContext
 

    if (type == "css") {
        return """<link rel="stylesheet" type="text/css" href="${appContext}${src}.css"/>\n""" 
    } else if(type == "javascript"){ 
        return """<script language="javascript" src="${appContext}${src}.js" charset="utf-8"></script>\n"""
    }
//    switch (Environment.current) {
//      case Environment.DEVELOPMENT:
//        break
//      case Environment.PRODUCTION:
//        return '<link rel="stylesheet" type="text/css" href="' + src + '-min.css"/>\n'
//        break
//    }
  }

  // jquery
  def jquery = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery-1.3.2');
  }

  // 提示成功的窗口
  def fancybox  = {attrs, body ->
    out << build("css",'/css/jquery.fancybox');
    out << build("javascript",'/js/jquery/jquery.fancybox');
  }
  // 提示成功的窗口
  def jgrowl = {attrs, body ->
    out << build("css",'/css/jquery.jgrowl');
    out << build("javascript",'/js/jquery/jquery.jgrowl');
  }

  // 用户详情的那个弹出
  def cluetip = {attrs, body ->
    out << build("css",'/css/jquery.cluetip');
    out << build("javascript",'/js/jquery/jquery.cluetip');
  }

  // 模板
  def jtemplates = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery-jtemplates');

  }

  // ajaxupload
  def ajaxupload = {attrs, body ->
    out << build("javascript",'/js/jquery/ajaxupload');

  }

  // 弹出的提交窗口
  def thickbox = {attrs, body ->
    out << build("css",'/css/thickbox');
    out << build("javascript",'/js/jquery/thickbox');

  }

  // 提示窗口，之前的那种黑色的大框框,可以去除了
  def tooltip = {attrs, body ->
    out << build("css",'/css/tooltip');
    out << build("javascript",'/js/jquery/tools.tooltip-1.0.2');

  }

  // 输入框自动完成
  def autocomplete = {attrs, body ->
    out << build("css",'/css/jquery.autocomplete');
    out << build("javascript",'/js/jquery/jquery.autocomplete');

  }

  // 定制多长时间后执行脚本的js
  def timeout = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.do-timeout');

  }

  // 操作cookies
  def cookies = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.cookies');
  }

  // 弹出窗口，用户首页表达积分的东西
  def jtoptip = {attrs, body ->
    out << build("css",'/css/jtoptip');
    out << build("javascript",'/js/jquery/jquery-jtoptip');

  }

  // 删除confirm的JS
  def confirm = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.confirm-1.2');

  }

  // ajax提交表单的JS
  def form = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.form');

  }

  // 美化表单的JS
  def jqtransform = {attrs, body ->
    out << build("css",'/css/jqtransform');
    out << build("javascript",'/js/jquery/jquery.jqtransform');

  }

  // 验证框架的JS
  def formValidator = {attrs, body ->
    out << build("css",'/css/validator');
    out << build("javascript",'/js/jquery/formValidator');
    out << build("javascript",'/js/jquery/formValidatorRegex');

  }

  // 星级评定系统的JS
  def rating = {attrs, body ->
    out << build("css",'/css/rating');
    out << build("javascript",'/js/jquery/jquery.rating');

  }

  //引用每一个domain下的js
  def domain = {attrs, body ->
    def domain = attrs.name ? attrs.name : "";
    if (domain) {
      out << build("javascript","/js/domainjs/${domain}");
    }
  }

  // 每一个页面都要用到的css文件
  def appcss = {attrs, body ->
    out << build("css",'/css/my_layout');
    out << build("css",'/css/topdiv');
    out << build("css",'/css/headerfooter');

  }

  // 用户后台所有页面公用的css
  def usercss = {attrs, body ->
    out << build("css",'/css/my_layout');
    out << build("css",'/css/topdiv');
    out << build("css",'/css/user_headerfooter');
    out << build("css",'/css/myspace');

  }
    // 管理员后台所有页面公用的css
  def admincss = {attrs, body ->
    out << build("css",'/css/my_layout');
    out << build("css",'/css/topdiv');
    out << build("css",'/css/user_headerfooter');
    out << build("css",'/css/admin_myspace');

  }
  //首页 index.css的引用
  def index = {attrs, body ->
    out << build("css",'/css/index');
    out << build("javascript",'/js/jquery/jquery.newsticker');

  }
  //疾病首页 sickindex.css的引用
  def sickindex = {attrs, body ->
    out << build("css",'/css/sickindex');
    out << build("javascript",'/js/jquery/jquery.newsticker');

  }
  //列表页面 list.css的引用
  def listcss = {attrs, body ->
    out << build("css",'/css/list');

  }

  // 医院内容页面mycontent.css的引用
  def yycontentcss = {attrs, body ->
    out << build("css",'/css/mycontent');

  }

  // 疾病内容页面mycontent.css的引用
  def sickcontentcss = {attrs, body ->
    out << build("css",'/css/mycontentbz');

  }

  // 药品组合内容页面mycontentzuhe.css的引用
  def zhcontentcss = {attrs, body ->
    out << build("css",'/css/mycontentzuhe');

  }

  // 排行页面mytop.css的引用
  def mytop = {attrs, body ->
    out << build("css",'/css/mytop');

  }

  // 弹出框twindow.css的引用
  def twindow = {attrs, body ->
    out << build("css",'/css/twindow');

  }

  // 过去时间
  def timeago = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.timeago');

  }
  // 验证密码强度
  def strength = {attrs, body ->
    out << build("javascript",'/js/jquery/pwd_strength');

  }
  // 权限验证
  def authAndUser = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.authanduser');

  }
  // comfirm
  def impromptu = {attrs, body ->
    out << build("css",'/css/jquery-impromptu.2.7');
    out << build("javascript",'/js/jquery/jquery-impromptu.2.7');

  }

  // comfirm
  def newsticker = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.newsticker');
  }

  // comfirm
  def url = {attrs, body ->
    out << build("javascript",'/js/jquery/jquery.url');
  }


  def lightbox = {attrs, body ->
    out << build("css",'/css/jquery.lightbox');
    out << build("javascript",'/js/jquery/jquery.lightbox');
  }
}
